home *** CD-ROM | disk | FTP | other *** search
/ The AGA Experience 3 / AGA Experience Volume 3 (1997)(NFA - SAdENESS)[!].iso / software / utilities / misc / finder / source / finder.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-13  |  30.2 KB  |  1,070 lines

  1. /**************************
  2. **
  3. ** $Id:  Finder.c
  4. ** $VER: Finder.c V2.06 (04.10.96) by Chris Jones
  5. **
  6. ** File Finder Utility with wildcards.
  7. ** Based on Amiga Shopper Tutorial by Toby Simpson.
  8. **
  9. ** Creation Date: 17.01.96
  10. ** Revision History:
  11. **                2.00 : (17.01.96) Initial version (beta release)
  12. **                2.01 : (18.01.96) Added AppMenu support
  13. **                                   (Public Release #1 - AUI May '96)
  14. **                2.02 : (04.02.96) Added option to start program
  15. **                                   iconified via tooltype.
  16. **                                   (Beta Release)
  17. **                2.03 : (04.04.96) Added 'Busy Pointer' while
  18. **                                   searching for files.
  19. **                                   (Not Released)
  20. **                2.04 : (19.07.96) Finder's window will now open on
  21. **                                   the default public screen, not
  22. **                                   on Workbench. (Workbench is
  23. **                                   usually the default public
  24. **                                   screen, but not always).
  25. **                                  When the Zoom gadget is used, the
  26. **                                   ListView no longer forgets what it
  27. **                                   was displaying.
  28. **                                  Major bug fix - In previous
  29. **                                   versions, the Listview would
  30. **                                   'forget' any filenames which left
  31. **                                   the current view.
  32. **                                  Source cleaned up a bit. Some of
  33. **                                   functions generated by GadToolsBox
  34. **                                   which have to be modified have been
  35. **                                   moved from "Finder_GUI.c" to 
  36. **                                   "Finder.c". This makes redesigning
  37. **                                   the GUI easier as the file needs
  38. **                                   less editing.
  39. **                                   NOTE: See "Finder_GUI.c" for the
  40. **                                          Instructions on the changes
  41. **                                          necessary.
  42. **                                   (Not Released)
  43. **                2.05 : (27.07.96) MAJOR bug fix. The IDCMP handler has
  44. **                                   been altered so that is uses no CPU
  45. **                                   time at all instead of (as was the
  46. **                                   case in earlier versions) swallowing
  47. **                                   all spare CPU time and thereby
  48. **                                   causing slow down on everything else.
  49. **                                  Totally new error handling system.
  50. **                                   Up until now I never tested Finder
  51. **                                   in any situation which might cause
  52. **                                   it to fail. When I did, I noticed
  53. **                                   that the error handling system was
  54. **                                   massively flawed. I was using the
  55. **                                   function "sprintf" which copies text
  56. **                                   to a variable, but I was passing it
  57. **                                   a file pointer (stderr). If any of
  58. **                                   the error messages were called this
  59. **                                   caused a crash (e.g. If it is run on
  60. **                                   <OS3.0). Now, Finder opens a small
  61. **                                   CON: window as soon as any faults
  62. **                                   appear and leaves the window until
  63. **                                   the user closes it. It also gives
  64. **                                   a rough idea of a possible solution
  65. **                                   to the problem.
  66. **                                  Changed the ListView from being Read-
  67. **                                   Only to the Show Selected option.
  68. **                                   This has no effect on the operation
  69. **                                   of Finder, but it does mean that
  70. **                                   when you are trying to remember the
  71. **                                   name/path of a file in the display
  72. **                                   while scrolling around, you can mark
  73. **                                   it by clicking on it and it will
  74. **                                   change colour to blue and remain so.
  75. **                                   (I only added this as I thought it
  76. **                                   might be useful).
  77. **                                   (Public Release #2 - Aminet)
  78. **                2.06 : (04.10.96) Nothing particularly new, just added
  79. **                                   the checking for a keyfile. However,
  80. **                                   the program IS NOT shareware, the
  81. **                                   keys are available freely from me.
  82. **                                   This is simply a way of keeping track
  83. **                                   of how many people (if any!) use
  84. **                                   Finder and if it is worth me 
  85. **                                   continuing development. If you use it
  86. **                                   PLEASE register it. IT IS FREE!!!
  87. **                                  Ooops! Just noticed another silly bug!
  88. **                                   If your started Finder via wb, it would
  89. **                                   open the libraries twice (silly me!)
  90. **                                   I only spotted this because the
  91. **                                   registration req. is in the library
  92. **                                   opening routine, so it popped up twice!
  93. **                                   This has now been removed!
  94. **
  95. **
  96. ** To compile, type "dcc -3.0 finder.c -o Finder" at a command line.
  97. **  and if you have DICE and the Exec 3.0 includes installed,
  98. **  it will compile.
  99. ** If you want CPU optimisation, use -020 or -030 options in DICE
  100. **
  101. ** VERY IMPORTANT: If you are using GadToolsBox and wish to alter the
  102. **  GUI and re-generate it's source code you must first consult the
  103. **  "ReadMe" file in the "Source" directory.
  104. **
  105. ** Wherever possible, I have put comments in the source so it
  106. ** should be fairly easy to follow.
  107. **
  108. ** My very largest thanks _MUST_ go to Toby Simpson who wrote the
  109. ** C Tutorial that this is based on. He is entirely responsible
  110. ** for my desire to program. I saw the first 'episode' of his
  111. ** tutorial and knew I had bought 'Complete Amiga C' (by Cliff
  112. ** Ramshaw) for a good reason. After many months playing with the
  113. ** very excellent 'GadToolsBox' (nice one Jan) with not a ROM
  114. ** Kernel manual in sight (not easy!) my Finder was born.
  115. ** Without Toby, this would be a program which did nothing except
  116. ** appear in the 'Tools' menu and read ToolTypes (not very useful)!
  117. **
  118. ** However, I would like to complain that the original GUI that
  119. ** Toby used was ridiculously complicated (it scaled to fit the
  120. ** screen it was on). GadTools provides a perfectly good solution
  121. ** to this; If Finder is unable to open it's screen/window, it will
  122. ** "fall back" onto a custom screen. Also, the program GUI was not
  123. ** finished when I stopped reading Amiga Shopper (i.e. the Cancel
  124. ** button).
  125. **
  126. ** Other than that, cheers Toby, I would like to buy you a pint.
  127. **
  128. **                                                       Chris.
  129. */
  130.  
  131. /* Includes, defines, etc. */
  132. #include <stdio.h>
  133. #include <stdlib.h>
  134. #include <string.h>
  135.  
  136. #include <dos/dos.h>
  137. #include <dos/dostags.h>
  138. #include <exec/exec.h>
  139. #include <exec/libraries.h>
  140. #include <exec/types.h>
  141. #include <intuition/intuition.h>
  142. #include <intuition/classes.h>
  143. #include <intuition/classusr.h>
  144. #include <intuition/imageclass.h>
  145. #include <intuition/gadgetclass.h>
  146. #include <libraries/gadtools.h>
  147. #include <graphics/displayinfo.h>
  148. #include <graphics/gfxbase.h>
  149. #include <clib/dos_protos.h>
  150. #include <clib/alib_protos.h>
  151. #include <clib/exec_protos.h>
  152. #include <clib/intuition_protos.h>
  153. #include <clib/gadtools_protos.h>
  154. #include <clib/graphics_protos.h>
  155. #include <clib/utility_protos.h>
  156. #include <clib/wb_protos.h>
  157. #include <workbench/workbench.h>
  158. #include <workbench/startup.h>
  159.  
  160. #define HELP_RAW_KEY 95 /* Raw key code for 'HELP' */
  161.  
  162. #include "Finder_GUI.c"
  163.  
  164. /* Prototype funcrions */
  165. extern int  wbmain(struct WBStartup *wbs);
  166. extern void OpenLibs(void);
  167. extern void main(int argc, char argv[]);
  168. extern void CloseResources(void);
  169. extern void About_Req(void);
  170. extern void GUI_Find(void);
  171. extern void BusyPointer(struct Window *FinderWnd, struct Requester *myReq);
  172. extern void NormalPointer(struct Window *FinderWnd, struct Requester *myReq);
  173. extern BOOL SearchDir(char *directory, char *pattern);
  174. extern BOOL NotifyFind(char *file, BOOL log_find);
  175. extern void ClearGUI_List(void);
  176. extern void AppMenuHide(void);
  177. extern void VanillaPressed(char vanilla_value);
  178. extern void RawPressed(char raw_value);
  179. extern void GadgetPressed(char gadget_value);
  180. extern int RegisterNagReq(void);
  181.  
  182. /* Global variables. */
  183. long  files_matched = 0;   /* Total # of files found */
  184. char  *VERSION = "\0$VER: Finder V2.06 (04.10.96)"; /* Version string */
  185. char  fname[256];
  186. char  Name[256];
  187. char  Path[256];
  188. extern struct DiskObject *def_assign;
  189. int   result = NULL; /* Variable used when calling certain fuctions */
  190. struct List            find_list;
  191. struct IntuiMessage   *m;
  192. char                   key_pressed;
  193. char                   help_command[256];
  194. BOOL                   running = TRUE;
  195. SHORT                  Id;
  196. FILE                  *error_pointer;
  197.  
  198.  
  199. /* Library bases */
  200. extern struct Library *SysBase;
  201. struct Library *GadToolsBase  = NULL;
  202. struct Library *IntuitionBase = NULL;
  203. struct Library *UtilityBase   = NULL;
  204. struct GfxBase *GfxBase       = NULL;
  205. struct Library *WorkbenchBase = NULL;
  206. struct Library *IconBase      = NULL;
  207.  
  208. /***************
  209. **
  210. ** int wbmain(struct WBStartup *wbs)
  211. **
  212. ** Workbench entry point.
  213. **
  214. ** This makes Finder run from Wb.
  215. **
  216. ** This function is messy because Tooltypes are case-sensitive
  217. ** so you need to check for any different spellings.
  218. **
  219. */
  220. int wbmain(struct WBStartup *wbs)
  221. {
  222.  
  223.    BOOL menu = FALSE;
  224.    BPTR oldlock;
  225.    struct WBArg *wba;
  226.    struct DiskObject *dob;
  227.    UBYTE **ta, *s;
  228.  
  229.    /* Open Libraries */
  230.    OpenLibs();
  231.  
  232.    /* Check our Tooltypes */
  233.       wba = wbs->sm_ArgList;
  234.       if (wbs->sm_NumArgs > 1)
  235.          {
  236.           NameFromLock(wba[1].wa_Lock,(STRPTR)fname,256);
  237.           AddPart((STRPTR)fname,(STRPTR)wba[1].wa_Name,256);
  238.           oldlock = CurrentDir(wba[1].wa_Lock);
  239.           dob     = (struct DiskObject *)GetDiskObject(wba[1].wa_Name);
  240.           CurrentDir(oldlock);
  241.           if (dob)
  242.            {
  243.             if (dob->do_ToolTypes)
  244.                {
  245.                 ta = (UBYTE **)dob->do_ToolTypes;
  246.                 if (s = (UBYTE *)FindToolType((char **)ta,"ACTION"))
  247.                  {
  248.                   if (MatchToolValue(s,(UBYTE *)"MENU"))       menu = TRUE; /* Here is the case stuff */
  249.                   if (MatchToolValue(s,(UBYTE *)"menu"))       menu = TRUE; /* (mENu, mEnu, meNu, etc.*/
  250.                   if (MatchToolValue(s,(UBYTE *)"Menu"))       menu = TRUE; /* are not checked! */
  251.                  }
  252.                }
  253.            }
  254.          }
  255.        else
  256.          {
  257.           oldlock = CurrentDir(wba->wa_Lock);
  258.           dob     = (struct DiskObject *)GetDiskObject(wba->wa_Name);
  259.           CurrentDir(oldlock);
  260.           if (dob)
  261.              {
  262.               if (dob->do_ToolTypes)
  263.                  {
  264.                   ta = (UBYTE **)dob->do_ToolTypes;
  265.                   if (s = (UBYTE *)FindToolType((char **)ta,"ACTION"))
  266.                      {
  267.                       if (MatchToolValue(s,(UBYTE *)"MENU"))    menu = TRUE;
  268.                       if (MatchToolValue(s,(UBYTE *)"menu"))    menu = TRUE;
  269.                       if (MatchToolValue(s,(UBYTE *)"Menu"))    menu = TRUE;
  270.                      }
  271.                  }
  272.               FreeDiskObject(dob);
  273.              };
  274.           }
  275.  
  276.    if (menu)
  277.       {
  278.        /* User wants us to enter the Tools menu */
  279.        AppMenuHide();
  280.  
  281.        /* User must have selected the AppMenuItem so, run the IDCMP handler */
  282.        NewList(&find_list);
  283.        do
  284.          {
  285.           result = HandleFinderIDCMP();
  286.          }
  287.        while (result!=0);
  288.  
  289.        /* If we are here, the user must have quit */
  290.        CloseResources();
  291.        exit(0);
  292.        return;
  293.       }
  294.  
  295.    else
  296.       {
  297.       return(main(0,0));
  298.       }
  299.  
  300.    return;
  301.  
  302. }
  303.  
  304. /*****************
  305. **
  306. ** void OpenLibs(void)
  307. **
  308. ** Function to open required libraries
  309. **
  310. */
  311. void OpenLibs(void)
  312. {
  313.  
  314.    /* Open libraries */
  315.    if (!(IntuitionBase = OpenLibrary("intuition.library", 39L)))
  316.       {
  317.        fprintf(error_pointer, "Can't open intuition.library V39+\n");
  318.        fprintf(error_pointer, " Check you are using OS3.0+\n");
  319.        exit(0);
  320.       }
  321.  
  322.    /* Ooooo, new routine! */
  323.    /* Before we do any more of the old lib opening */
  324.    /* Let's find out if Finder has been registered...*/
  325.    FILE *finder_pointer;
  326.  
  327.    if (!(finder_pointer=fopen("s:Finder.key","r")))
  328.       {
  329.          RequesterNagReq();
  330.       }
  331.    fclose(finder_pointer);
  332.  
  333.     if (!(GadToolsBase = OpenLibrary("gadtools.library", 39L)))
  334.       {
  335.        fprintf(error_pointer, "Can't open gadtools.library V39+\n");
  336.        fprintf(error_pointer, " Check you are using OS3.0+\n");
  337.        CloseLibrary(IntuitionBase);
  338.        exit(0);
  339.       }
  340.     if (!(UtilityBase = OpenLibrary("utility.library", 39L)))
  341.       {
  342.        fprintf(error_pointer, "Can't open utility.library V39+\n");
  343.        fprintf(error_pointer, " Check you are using OS3.0+\n");
  344.        CloseLibrary(IntuitionBase);
  345.        CloseLibrary(GadToolsBase);
  346.        exit(0);
  347.       }
  348.     if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39L)))
  349.       {
  350.        fprintf(error_pointer, "Can't open graphics.library V39+\n");
  351.        fprintf(error_pointer, " Check you are using OS3.0+\n");
  352.        CloseLibrary(IntuitionBase);
  353.        CloseLibrary(GadToolsBase);
  354.        CloseLibrary(UtilityBase);
  355.        exit(0);
  356.       }
  357.     if (!(IconBase = OpenLibrary("icon.library", 39L)))
  358.       {
  359.        fprintf(error_pointer, "Can`t open icon.library V39+\n");
  360.        fprintf(error_pointer, " Check you are using OS3.0+\n");
  361.        CloseLibrary(IntuitionBase);
  362.        CloseLibrary(GadToolsBase);
  363.        CloseLibrary(UtilityBase);
  364.        CloseLibrary((struct Library *)GfxBase);
  365.        exit(0);
  366.       }
  367.  
  368.    /* Setup HELP command */
  369.    sprintf(help_command, "run <NIL: >NIL: Sys:Utilities/Multiview HELP:Finder.guide");
  370.  
  371.    /* All done, so return */
  372.    return;
  373.  
  374. }
  375.  
  376. /***************
  377. **
  378. ** void main (int argc, char argv[])
  379. **
  380. ** CLI Entry Point
  381. **
  382. ** This function is called by CLI entry and the wbmain() function.
  383. ** It basically sets up the window and does the
  384. **  input loop (IDCMP Handler).
  385. **
  386. */
  387. void main(int argc, char argv[])
  388. {
  389.  
  390.    /* Setup Error Output system */
  391.    if(!(error_pointer=fopen("CON:10/10/320/80/FinderError/AUTO/CLOSE/WAIT","w")))
  392.       {
  393.       fclose(error_pointer);
  394.       CloseResources();
  395.       exit(0);
  396.       }
  397.  
  398.    /* Open our libraries */
  399.    if(GfxBase==NULL)
  400.       {
  401.       OpenLibs();
  402.       }
  403.  
  404.    /* Since we were called from CLI (or any icon work is finished), we don`t need icon.library */
  405.    if (IconBase)  CloseLibrary(IconBase);
  406.  
  407.    /* Call function to set the screen up properly */
  408.    result = SetupScreen();
  409.    if (result!=0)
  410.       {
  411.        fprintf(error_pointer, "Error opening screen\n");
  412.        fprintf(error_pointer, " Check you have Workbench open\n or there is another screen\n defined as the default public\n");
  413.        CloseResources();
  414.        exit(0);
  415.       }
  416.  
  417.    /* Call function to open our window */
  418.    result = OpenFinderWindow();
  419.    if (result!=0)
  420.       {
  421.        fprintf(error_pointer, "Error opening window\n");
  422.        fprintf(error_pointer, " Check the resolution of this screen\n If it is too small,\n Finder will not run, but\n should have opened its\n own screen.\n");
  423.        CloseResources();
  424.        exit(0);
  425.       }
  426.  
  427.    /* Activate our window */
  428.    /* The window should have the GT_ACTIVATE tag, but this makes sure */
  429.    ActivateWindow(FinderWnd);
  430.  
  431.    /* Initialise the file list and clear the listview (should by empty anyway) */
  432.    ClearGUI_List();
  433.  
  434.    /* Put up welcome message to user */
  435.    NotifyFind("", FALSE);
  436.    NotifyFind("", FALSE);
  437.    NotifyFind("        Finder V2.06 by Chris Jones", FALSE);
  438.    NotifyFind("        ©1996 Dynamic Software", FALSE);
  439.    NotifyFind("        All Rights Reserved", FALSE);
  440.    NotifyFind("", FALSE);
  441.    NotifyFind("        For more information, please see the docs", FALSE);
  442.    NotifyFind("         or press 'HELP'.", FALSE);
  443.  
  444.    /* Call IDCMP handler loop function */
  445.    do
  446.      {
  447.       result = HandleFinderIDCMP();
  448.      }
  449.    while (result!=0);
  450.  
  451.    /* If we are here, the user has selected a quit so we need to close down */
  452.    CloseResources();
  453.  
  454.    /* Close our error window */
  455.    fclose(error_pointer);
  456.  
  457.    /* Everything is finished and closed */
  458.    return;
  459.  
  460. }
  461.  
  462. /**************
  463. **
  464. ** int HandleFinderIDCMP( void )
  465. **
  466. ** Handles all incoming messages from Intuition (e.g. button clicks, etc)
  467. **
  468. ** Returns "FALSE" if the program needs to be quit
  469. ** All other functions (e.g. redraws, zooming, finding, etc.) are handled
  470. **  from this function. (Not very efficient, but time saving!)
  471. **
  472. */
  473.  
  474. int HandleFinderIDCMP( void )
  475. {
  476.  
  477.    WaitPort(FinderWnd->UserPort);
  478.    while( m = GT_GetIMsg( FinderWnd->UserPort )) {
  479.  
  480.       CopyMem(( char * )m, ( char * )&FinderMsg, (long)sizeof( struct IntuiMessage ));
  481.  
  482.       Id = ((struct Gadget *)(m->IAddress))->GadgetID;
  483.       GT_ReplyIMsg( m );
  484.  
  485. switch ( FinderMsg.Class ) {
  486.  
  487.    case  IDCMP_REFRESHWINDOW:
  488.          GT_BeginRefresh( FinderWnd );
  489.          GT_EndRefresh( FinderWnd, TRUE );
  490.    break;
  491.  
  492.    case  IDCMP_CLOSEWINDOW:
  493.          AppMenuHide();
  494.    break;
  495.  
  496.    case  IDCMP_VANILLAKEY:
  497.          VanillaPressed(m->Code);
  498.    break;
  499.  
  500.    case  IDCMP_RAWKEY:
  501.          /* Call RAW key handler */
  502.          RawPressed(m->Code);
  503.    break;
  504.  
  505.    case  IDCMP_GADGETUP:
  506.          /* Call gadget handler */
  507.          GadgetPressed(Id);
  508.    break;
  509.  
  510. break;
  511.       }
  512.    }
  513.    return( running );
  514. }
  515.  
  516. /****************
  517. **
  518. ** void VanillaPressed(char vanilla_value)
  519. **
  520. ** Deals with any key strokes within the normal ASCII set
  521. **
  522. */
  523. void VanillaPressed(char vanilla_value)
  524. {
  525.  
  526. /* Key presses don't need to be case-sensetive */
  527. /* Therefore, convert upper case to lower; thus */
  528. key_pressed = vanilla_value;
  529. if (key_pressed >= 'A' && key_pressed <='Z')
  530.    key_pressed = key_pressed + 0x20;
  531.  
  532. /* Act on the key press */
  533. switch(key_pressed)
  534.    {
  535.    case 'f': /* FIND button has been chosen */
  536.       GUI_Find();
  537.    break;
  538.  
  539.    case 'q': /* QUIT button has been chosen */
  540.       running = FALSE;
  541.    break;
  542.  
  543.    case 'h': /* HELP button has been chosen */
  544.       SystemTags(help_command, SYS_Input, NULL, SYS_Output, NULL, TAG_DONE);
  545.    break;
  546.  
  547.    case 'a': /* ABOUT button has been chosen */
  548.       About_Req();
  549.    break;
  550.  
  551.    case 's': /* Search dir string gadget has been chosen */
  552.       ActivateGadget(FinderGadgets[GD_path], FinderWnd, NULL);
  553.    break;
  554.  
  555.    case 'p': /* Pattern string gadget has been chosen */
  556.       ActivateGadget(FinderGadgets[GD_pattern], FinderWnd, NULL);
  557.    break;
  558.    }
  559.  
  560. return;
  561.  
  562. }
  563.  
  564. /****************
  565. **
  566. ** void RawPressed(char raw_value)
  567. **
  568. ** Function to deal with  any non-ASCII key presses
  569. **  Currently, only 'Help' is supported
  570. **
  571. */
  572. void RawPressed(char raw_value)
  573. {
  574.  
  575. switch(raw_value)
  576.    {
  577.    case HELP_RAW_KEY: /* Help button */
  578.       SystemTags(help_command, SYS_Input, NULL, SYS_Output, NULL, TAG_DONE);
  579.    break;
  580.  
  581. break;   
  582.    }
  583.  
  584. return;
  585.  
  586. }
  587.  
  588. /****************
  589. **
  590. ** void GadgetPressed(char gadget_value)
  591. **
  592. ** Function to deal with any gadget presses
  593. **
  594. */
  595. void GadgetPressed(char gadget_value)
  596. {
  597.  
  598. switch(gadget_value)
  599.    {
  600.    case GD_quit:
  601.       running = FALSE;
  602.    break;
  603.  
  604.    case GD_find:
  605.       GUI_Find();
  606.    break;
  607.  
  608.    case GD_about:
  609.       About_Req();
  610.    break;
  611.  
  612.    case GD_help:
  613.       SystemTags(help_command, SYS_Input, NULL, SYS_Output, NULL, TAG_DONE);
  614.    break;
  615.    }
  616.  
  617. return;
  618.  
  619. }
  620.  
  621.  
  622. /****************
  623. **
  624. ** void CloseResources();
  625. **
  626. ** Function to close screen, window, libraries, etc.
  627. **
  628. */
  629. void CloseResources(void)
  630. {
  631.  
  632.    if (FinderWnd)             CloseFinderWindow();
  633.    if (Scr)                   CloseDownScreen();
  634.    if (IntuitionBase)         CloseLibrary(IntuitionBase);
  635.    if (GadToolsBase)          CloseLibrary(GadToolsBase);
  636.    if (UtilityBase)           CloseLibrary(UtilityBase);
  637.    if (GfxBase)               CloseLibrary((struct Library *)GfxBase);
  638.    if (IconBase)              CloseLibrary(IconBase);
  639.    if (SysBase)               CloseLibrary(SysBase);
  640.  
  641. return;
  642.  
  643. }
  644.  
  645. /*******************
  646. **
  647. ** void About_Req(void);
  648. **
  649. ** Function to display simple about requester.
  650. ** This uses the _VERY_ simple 'EasyRequest'
  651. **
  652. */
  653. void About_Req( void )
  654. {
  655.    /* Set up information for requester */
  656.    struct Requester tinyReq;
  657.    struct EasyStruct about =
  658.       {
  659.        sizeof(struct EasyStruct),
  660.        0,
  661.        "About Finder",
  662.  
  663.        "Finder V2.06\nBy Chris Jones\n©1996 Dynamic Software\nAll rights reserved.\n\nFor more information, see docs or press 'Help'.\n\nFinder is FREEWARE.\nPlease copy it to all your friends.\n\nTo contact author, see docs\n",
  664.        "Continue"
  665.        };
  666.  
  667.    /* Call requester */
  668.    BusyPointer(FinderWnd, &tinyReq);
  669.    EasyRequest(NULL, &about, NULL, NULL);
  670.    NormalPointer(FinderWnd, &tinyReq);
  671.  
  672.    return;
  673.  
  674. }
  675.  
  676. /*********************
  677. **
  678. ** void BusyPointer(struct Window *FinderWnd, struct Requester *myReq)
  679. **
  680. ** Function to put a busy pointer on main window if 'About' is displayed
  681. ** and while the GUI_Find() function is operating.
  682. ** This is so the user can't carry on using the program while there are
  683. **  about requesters showing. (This would cause problems).
  684. **
  685. */
  686. void BusyPointer(struct Window *FinderWnd, struct Requester *myReq)
  687. {
  688.  
  689.    InitRequester(myReq);
  690.    Request(myReq, FinderWnd);
  691.    SetWindowPointer(FinderWnd,  WA_BusyPointer, TRUE,
  692.                                 WA_PointerDelay,   TRUE,
  693.                                 TAG_END);
  694.    return;
  695.  
  696. }
  697.  
  698. /********************
  699. **
  700. ** void NormalPointer (struct Window *FinderWnd, struct Requester *myReq)
  701. **
  702. ** Function to restore pointer to normal.
  703. **
  704. */
  705. void NormalPointer(struct Window *FinderWnd, struct Requester *myReq)
  706. {
  707.  
  708.    ClearPointer(FinderWnd);
  709.    EndRequest(myReq, FinderWnd);
  710.  
  711. return;
  712.  
  713. }
  714.  
  715. /**************************
  716. **
  717. ** BOOL  SearchDir(char *directory, char *pattern)
  718. **
  719. ** Search the named directory. All variables are local, this function
  720. ** is recursive. Returns TRUE if the operation was OK, or FALSE for an
  721. ** error.
  722. **
  723. ** This function is a shameless copy of Toby Simpson's function
  724. */
  725.  
  726. BOOL  SearchDir(char *directory, char *pattern)
  727. {
  728.  
  729.    __aligned struct  FileInfoBlock     fib;
  730.  
  731.    BPTR     lk    = NULL;
  732.    char     full_path[255];
  733.  
  734.    /*
  735.    ** Attempt to get a lock and the initial FIB:
  736.    */
  737.    if (!(lk = Lock(directory, ACCESS_READ)))
  738.       {
  739.       fprintf(error_pointer, "Error accessing directory\n");
  740.       return FALSE;
  741.       }
  742.  
  743.    if (!(Examine(lk, &fib)))
  744.       {
  745.       fprintf(error_pointer, "Error accessing FileInfoBlock\n");
  746.       return FALSE;
  747.       }
  748.  
  749.    /*
  750.    ** Scan directory:
  751.    */
  752.    while (ExNext(lk, &fib))
  753.       {
  754.       /*
  755.       ** Deal with CTRL-C (from CLI or 'break'):
  756.       */
  757.     if (SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)
  758.       {
  759.       fprintf(error_pointer, "Received a Ctrl-C break\n Search stopped\n");
  760.          UnLock(lk);
  761.       return FALSE;
  762.       }
  763.  
  764.       /*
  765.       ** Build full path spec:
  766.       */
  767.       strcpy(full_path, directory);
  768.       AddPart(full_path, fib.fib_FileName, 255);
  769.  
  770.       /*
  771.       ** ID File entry type:
  772.       */
  773.       if (fib.fib_DirEntryType > 0)
  774.          {
  775.          /*
  776.          ** Got a directory, recursively scan it:
  777.          */
  778.          if (!(SearchDir(full_path, pattern)))
  779.             {
  780.             UnLock(lk);
  781.             return FALSE;
  782.             }
  783.          }
  784.       else
  785.          {
  786.          /*
  787.          ** Got a file, try a match check:
  788.          */
  789.          if (MatchPatternNoCase(pattern, fib.fib_FileName))
  790.             NotifyFind(full_path, TRUE);
  791.          }
  792.       }
  793.  
  794.    UnLock(lk);
  795.  
  796.    return TRUE;
  797.  
  798. }
  799.  
  800. /**************************
  801. **
  802. ** BOOL  NotifyFind(char *file, BOOL log_find)
  803. **
  804. ** Notify that a file was found. The file which matched is passed in
  805. ** and this is then shown on the screen in what every way the program
  806. ** decides. Returns FALSE for an error.
  807. ** If "log_find" is TRUE, this match is counted.
  808. **
  809. ** This is also a copy of Toby Simpson's equivalent function.
  810. */
  811.  
  812. BOOL  NotifyFind(char *file, BOOL log_find)
  813. {
  814.  
  815.    struct   Node  *node;
  816.    char           *text_ptr;
  817.  
  818.    /*
  819.    ** Count matches if required:
  820.    */
  821.    if (log_find)  files_matched++;
  822.  
  823.    /*
  824.    ** Allocate memory for a list node:
  825.    */
  826.    if (!(node = (struct Node *)malloc(sizeof(struct Node))))
  827.       {
  828.       fprintf(error_pointer, "Error allocating memory.\n Close some other programs\n and try again\n");
  829.       return FALSE;
  830.       }
  831.  
  832.    if (!(text_ptr = malloc(strlen(file) + 1)))
  833.       {
  834.       free(node);
  835.       return FALSE;
  836.       }
  837.  
  838.    /*
  839.    ** Initialise our new node, and add it to the list:
  840.    */
  841.    strcpy(text_ptr, file);
  842.  
  843.    node->ln_Name = text_ptr;
  844.    node->ln_Pri  = 0;
  845.  
  846.    AddTail(&find_list, node);
  847.  
  848.    /*
  849.    ** Update the display gadget so it shows the new list:
  850.    */
  851.    GT_SetGadgetAttrs(FinderGadgets[GD_filelister], FinderWnd, NULL,
  852.       (GTLV_Labels), &find_list,
  853.       (GTLV_Top),    files_matched,
  854.       (TAG_DONE));
  855.  
  856.    return TRUE;
  857.  
  858. }
  859.  
  860. /**************************
  861. **
  862. ** void  ClearGUI_List(void)
  863. **
  864. ** Clears out any memory occupied by the list we build of any matches
  865. ** found during the find operation.
  866. **
  867. ** Guess what, Toby Simpson wrote one quite similar to this!
  868. */
  869.  
  870. void  ClearGUI_List(void)
  871. {
  872.  
  873.    struct   Node  *node;
  874.  
  875.    /*
  876.    ** Clear out list if anything in it:
  877.    */
  878.    while (node = RemHead(&find_list))
  879.       {
  880.       free(node->ln_Name);
  881.       free(node);
  882.       }
  883.  
  884.    NewList(&find_list);    /* Re-initialise list in case it is used again */
  885.  
  886.    return;
  887.  
  888. }
  889.  
  890. /**************************
  891. **
  892. ** void  GUI_Find(void)
  893. **
  894. ** This routine is responsible for triggering a find operation. It is
  895. ** called if the FIND button, or its keyboard short-cut is pressed.
  896. **
  897. ** Toby Simpson was 'ere
  898. */
  899.  
  900. void  GUI_Find( void )
  901. {
  902.  
  903.    char     search_dir[64];
  904.    char     search_string[64];
  905.    char     search_pattern[128];
  906.    char     work_string[128];
  907.  
  908.    /*
  909.    ** Clear the current list, if any:
  910.    */
  911.    ClearGUI_List();
  912.  
  913.    GT_SetGadgetAttrs(FinderGadgets[GD_filelister], FinderWnd, NULL,
  914.       (GTLV_Labels), NULL,
  915.       (TAG_DONE));
  916.    files_matched = 0;
  917.  
  918.    /*
  919.    ** Activate 'Busy Pointer'
  920.    */
  921.    SetWindowPointer(FinderWnd,
  922.                      WA_BusyPointer, TRUE,
  923.                      WA_PointerDelay, TRUE,
  924.                      TAG_END);
  925.  
  926.    /*
  927.    ** Pull out search strings:
  928.    */
  929.    NotifyFind("", FALSE);
  930.  
  931.    strcpy(search_dir,    ((struct StringInfo *)FinderGadgets[GD_path]->SpecialInfo)->Buffer);
  932.    strcpy(search_string, ((struct StringInfo *)FinderGadgets[GD_pattern]->SpecialInfo)->Buffer);
  933.    sprintf(work_string,  "Searching '%s' for files matching '%s'", search_dir, search_string);
  934.    NotifyFind(work_string, FALSE);
  935.  
  936.    /*
  937.    ** Pre-Parse the AmigaDOS search pattern:
  938.    */
  939.    ParsePatternNoCase(search_string, search_pattern, 127);
  940.  
  941.    /*
  942.    ** Start the search:
  943.    */
  944.    if (!(SearchDir(search_dir, search_pattern)))
  945.       NotifyFind("-- Operation not totally successful.", FALSE);
  946.    else
  947.       {
  948.       sprintf(work_string, "-- %ld matches found", files_matched);
  949.       NotifyFind(work_string, FALSE);
  950.       }
  951.  
  952.    /*
  953.    ** Return Pointer to normal
  954.    */
  955.    ClearPointer(FinderWnd);
  956.  
  957.    /*
  958.    ** Operation complete, return now.
  959.    */
  960.    return;
  961.  
  962. }
  963.  
  964. /*******************
  965. **
  966. ** void AppMenuHide( void )
  967. **
  968. ** Closes window and puts name in menu bar
  969. **
  970. ** This one is actually mine! (Well, it is based on the Aminet ROM
  971. ** Kernel examples!)
  972. */
  973. void AppMenuHide( void )
  974. {
  975.  
  976.    struct MsgPort      *myport  =NULL;
  977.    struct AppMenuItem  *appitem =NULL;
  978.    struct AppMessage   *appmsg  =NULL;
  979.    LONG count=0L;
  980.    BOOL success=0L;
  981.  
  982.    if (WorkbenchBase = OpenLibrary("workbench.library",39))
  983.      {
  984.      if(myport = CreateMsgPort())
  985.        {
  986.        /* Add our own AppMenuItem to the Workbench Tools Menu */
  987.        appitem=AddAppMenuItemA(0L,                   /* Our ID# for item */
  988.                                0L,                   /* Our UserData     */
  989.                               "Finder V2.06",        /* MenuItem Text    */
  990.                                myport,NULL);         /* MsgPort, no tags */
  991.  
  992.       /* Hide Finder by closing our window and screen */
  993.       if (FinderWnd) CloseFinderWindow();
  994.       if (Scr)       CloseDownScreen();
  995.  
  996.        if(appitem)
  997.          {
  998.  
  999.          /* For this program, we allow the AppMenuItem to be selected */
  1000.          /* only once, then we remove it and re-open Finder */
  1001.          WaitPort(myport);
  1002.          while((appmsg=(struct AppMessage *)GetMsg(myport)))
  1003.            {
  1004.            /* Handle messages from the AppMenuItem - we have only one  */
  1005.            /* item so we don't have to check its appmsg->am_ID number. */
  1006.            /* We find our userdata pointer in appmsg->am_UserData      */
  1007.    
  1008.            ReplyMsg((struct Message *)appmsg);
  1009.            }
  1010.          success=RemoveAppMenuItem(appitem);
  1011.          }
  1012.  
  1013.        /* Clear away any messages that arrived at the last moment */
  1014.        /* and let Workbench know we're done with the messages     */
  1015.        while(appmsg=(struct AppMessage *)GetMsg(myport))
  1016.          {
  1017.           ReplyMsg((struct Message *)appmsg);
  1018.          }
  1019.        DeleteMsgPort(myport);
  1020.        }
  1021.      CloseLibrary(WorkbenchBase);
  1022.      }
  1023.  
  1024.    /* Re-open and activate window and screen */
  1025.    SetupScreen();
  1026.    OpenFinderWindow();
  1027.    ActivateWindow(FinderWnd);
  1028.  
  1029.    /*
  1030.    ** Update the display gadget so it shows the new list:
  1031.    */
  1032.    GT_SetGadgetAttrs(FinderGadgets[GD_filelister], FinderWnd, NULL,
  1033.       (GTLV_Labels), &find_list,
  1034.       (GTLV_Top),    files_matched,
  1035.       (TAG_DONE));
  1036.  
  1037.  
  1038.    /* We're finished, so return */
  1039.    return;
  1040.  
  1041. }
  1042.  
  1043. /*******************
  1044. **
  1045. ** void RequesterNagReq(void);
  1046. **
  1047. ** Function to display simple nag requester.
  1048. ** This uses the _VERY_ simple 'EasyRequest'
  1049. **
  1050. */
  1051. int RequesterNagReq( void )
  1052. {
  1053.    /* Set up information for requester */
  1054.    struct EasyStruct nag =
  1055.       {
  1056.        sizeof(struct EasyStruct),
  1057.        0,
  1058.        "Registering Finder",
  1059.  
  1060.        "WARNING: You have not registered Finder V2.06\nTo do so IS FREE, YES FREE\n\nSend a mail to me and I will send\nyou the key.\n\nE-Mail: ee61cj@ee.surrey.ac.uk\n\n\nREMEMBER IT IS FREE.\n",
  1061.        "Continue"
  1062.        };
  1063.  
  1064.    /* Call requester */
  1065.    EasyRequest(NULL, &nag, NULL, NULL);
  1066.  
  1067.    return;
  1068.  
  1069. }
  1070.